//
//  MainTabBar.swift
//  02-weibo（swift第二遍）
//
//  Created by 刘涛 on 15/6/23.
//  Copyright © 2015年 tao6. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    
    
    lazy var composeButton: UIButton = {
        let btn = UIButton(type: UIButtonType.ContactAdd)
        
        // self. 闭包，提前准备好的代码，在需要的时候执行
        self.addSubview(btn)
        
        
        return btn
    }()
    
    
    // 按钮总数
    private let buttonCount: CGFloat = 5
    
    // 不要再layoutSubviews写太耗时操作
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // UITabBarButton 是私有接口，在发开不要使用
        // 遍历子视图

        
        let w = bounds.width / buttonCount
        
        let h = bounds.height
        
        let rect = CGRectMake(0, 0, w, h)
        
        var index: CGFloat = 0
        
        for v in subviews {
            if v is UIControl && !(v is UIButton) {
                v.frame = CGRectOffset(rect, index * w, 0)
                
                index += (index == 1) ? 2 : 1
                
            }
        }
        
        
        composeButton.frame = frame
        
        composeButton.center = CGPointMake(center.x, bounds.height * 0.5)
        
    }

}
